package com.fjec.market.schedule.handler;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.fjec.common.Const;
import com.fjec.market.entity.InfoBusiness;
import com.fjec.market.entity.InfoFarmer;
import com.fjec.market.entity.InfoPurchase;
import com.fjec.market.service.*;
import com.xxl.job.core.log.XxlJobLogger;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.MapUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

@Slf4j
@Component
public class ShopStarHandler {

    @Resource
    private IShopService shopService;

    @Resource
    private IInfoFarmerService farmerService;

    @Resource
    private IInfoBusinessService businessService;

    @Resource
    private IInfoPurchaseService purchaseService;

    public String handle() {
        int fnum = 0;
        int bnum = 0;
        int pnum = 0;
        try {
            fnum = this.handleFarmer();
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            XxlJobLogger.log(ex);
        }
        try {
            bnum = this.handleBusiness();
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            XxlJobLogger.log(ex);
        }
        try {
            pnum = this.handlePurchase();
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            XxlJobLogger.log(ex);
        }
        // 返回处理结果
        return String.format("FARMER=%d, BIZ=%d, PUR=%d", fnum, bnum, pnum);
    }

    /**
     * 处理果农
     *
     * @return
     */
    private int handleFarmer() {
        AtomicInteger count = new AtomicInteger(Const.NUMBER_ZERO);
        // 获取所有评分列表
        List<Map<String, Object>> mapList = shopService.getFarmerShopStar();
        // 更新商品评分
        mapList.forEach(map -> {
            try {
                Long busiId = MapUtils.getLong(map, "BusiId");
                Integer shopStar = MapUtils.getInteger(map, "ShopStar");
                UpdateWrapper<InfoFarmer> updateWrapper = new UpdateWrapper<>();
                updateWrapper.eq("Id", busiId);
                updateWrapper.ne("ShopStar", shopStar);
                InfoFarmer entity = new InfoFarmer();
                entity.setShopStar(shopStar);
                boolean result = farmerService.update(entity, updateWrapper);
                if (result) {
                    count.getAndIncrement();
                }
            } catch (Exception ex) {
                log.error(ex.getMessage(), ex);
            }
        });
        // 返回处理数量
        return count.intValue();
    }

    /**
     * 处理分拣、农资、包装
     *
     * @return
     */
    private int handleBusiness() {
        AtomicInteger count = new AtomicInteger(Const.NUMBER_ZERO);
        // 获取所有评分列表
        List<Map<String, Object>> mapList = shopService.getBusinessShopStar();
        // 更新商品评分
        mapList.forEach(map -> {
            try {
                Long busiId = MapUtils.getLong(map, "BusiId");
                Integer shopStar = MapUtils.getInteger(map, "ShopStar");
                UpdateWrapper<InfoBusiness> updateWrapper = new UpdateWrapper<>();
                updateWrapper.eq("Id", busiId);
                updateWrapper.ne("ShopStar", shopStar);
                InfoBusiness entity = new InfoBusiness();
                entity.setShopStar(shopStar);
                boolean result = businessService.update(entity, updateWrapper);
                if (result) {
                    count.getAndIncrement();
                }
            } catch (Exception ex) {
                log.error(ex.getMessage(), ex);
            }
        });
        // 返回处理数量
        return count.intValue();
    }

    /**
     * 处理采购商
     *
     * @return
     */
    private int handlePurchase() {
        AtomicInteger count = new AtomicInteger(Const.NUMBER_ZERO);
        // 获取所有评分列表
        List<Map<String, Object>> mapList = shopService.getPurchaseShopStar();
        // 更新商品评分
        mapList.forEach(map -> {
            try {
                Long busiId = MapUtils.getLong(map, "BusiId");
                Integer shopStar = MapUtils.getInteger(map, "ShopStar");
                UpdateWrapper<InfoPurchase> updateWrapper = new UpdateWrapper<>();
                updateWrapper.eq("Id", busiId);
                updateWrapper.ne("ShopStar", shopStar);
                InfoPurchase entity = new InfoPurchase();
                entity.setShopStar(shopStar);
                boolean result = purchaseService.update(entity, updateWrapper);
                if (result) {
                    count.getAndIncrement();
                }
            } catch (Exception ex) {
                log.error(ex.getMessage(), ex);
            }
        });
        // 返回处理数量
        return count.intValue();
    }
}
